package com.bygf.curve.panel;

import com.bygf.curve.utils.Utils;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 * 曲线标题面板
 *
 * @author sunyupei
 */
public class TitlePanel extends JPanel {

    //标题文本
    private String title = "标题";
    //标题字体样式
    private Font titleFont = new Font("宋体", Font.PLAIN, 12);
    //标题颜色
    private Color color = Color.BLACK;
    //标题问题宽度（像素），用于居中绘制
    private int titleWidth = 0;
    //标题问题高度（像素），用于居中绘制
    private int titleHeight = 12;
    //绘制时的边距
    private int margin = 5;

    /**
     * 构造方法
     *
     * @param title 标题文本
     */
    public TitlePanel(String title) {
        this.setLayout(null);
        this.setOpaque(false);
        setTitle(title);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        BufferedImage bufferedImage = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = bufferedImage.createGraphics();
        RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//        rh.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
        rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        rh.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
        rh.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2.setRenderingHints(rh);
        doubleBuffer(g2);
        g.drawImage(bufferedImage, 0, 0, null);
    }

    /**
     * 双缓冲绘制
     *
     * @param g
     */
    private void doubleBuffer(Graphics2D g) {
        g.setFont(titleFont);
        g.setColor(color);
        int x = (this.getWidth() - titleWidth) / 2;
        int y = titleHeight + margin;
        g.drawString(title, x, y);

    }

    /**
     * 设置标题文本，同时计算文本宽度（像素）
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
        titleWidth = Utils.getStringWidth(title, titleFont);
        this.repaint();
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @return the font
     */
    @Override
    public Font getFont() {
        return titleFont;
    }

    /**
     * 设置标题字体样式，同时计算标题文本宽度及高度（像素）
     *
     * @param font 字体样式
     */
    public void setTitleFont(Font font) {
        this.titleFont = font;
        titleWidth = Utils.getStringWidth(title, font);
        titleHeight = font.getSize();
        this.setSize(this.getWidth(), titleHeight + margin * 2);
    }

    /**
     * @return the color
     */
    public Color getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(Color color) {
        this.color = color;
    }

    @Override
    public void setSize(int width, int height) {
        super.setSize(width, titleHeight + margin * 2);
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, titleHeight + margin * 2);
    }
}
